/*
 * Serial.c
 *
 * Created: 3/27/2011 09:55:05
 *  Author: morpheus
 */
#include "uart.h"

void uart_init()
{
		/* Set the baud rate */
  UBRR0H = (unsigned char) (BAUD_PRESCALE >> 8);                  
  UBRR0L = (unsigned char) BAUD_PRESCALE;
  
  // using util/setbaud.h
  //UBRR0H = UBRRH_VALUE;
  //UBRR0L = UBRRL_VALUE;

	// clear the 2x bit
	UCSR0A &= ~(1 << 1);

  // Turn on the transmission and reception circuitry
  UCSR0B = (1 << RXEN0) | (1 << TXEN0); 
      
  // Use 8-bit character sizes - URSEL bit set to select the UCRSC register 
  UCSR0C = (1 << USBS0) | (1 << UCSZ01) | (1 << UCSZ00);
}
unsigned char uart_receive( void )
{
	/* Wait for incomming data */
	while ( !(UCSR0A & (1 << RXC0)) );			                
	/* Return the data */
	return UDR0;
}

void uart_putc(const char data)
{
	while ((UCSR0A & (1 << UDRE0)) == 0);
	
  UDR0 = data;
	
}
/*************************************************************************
Function: uart_puts()
Purpose:  transmit string to UART
Input:    string to be transmitted
Returns:  none          
**************************************************************************/
void uart_puts(const char *message)
{
    while (*message) 
      uart_putc(*message++);

}/* uart_puts */


/*************************************************************************
Function: uart_puts_p()
Purpose:  transmit string from program memory to UART
Input:    program memory string to be transmitted
Returns:  none
**************************************************************************/
void uart_puts_p(const char *progmem_s )
{
    register char c;
    
    while ( (c = pgm_read_byte(progmem_s++)) ) 
      uart_putc(c);

}/* uart_puts_p */
